<?php
if (!(defined('IN_IA'))) 
{
	exit('Access Denied');
}

//服务类设备
class Index_MxShopPage extends MobilePage {

    private $sGoodsTable = '';
    private $sGoodsBrandsTable = '';
    private $sGoodsTypeTable = '';
    private $mendianTable = '';
    private $sgoods = array();

    public function __construct()
    {
        parent::__construct();

        $this->sGoodsTable = tablename('mx_shop_service_goods');
        $this->sGoodsBrandsTable = tablename('mx_shop_service_goods_brands');
        $this->sGoodsTypeTable = tablename('mx_shop_service_goods_type');
        $this->mendianTable = tablename('mx_shop_mendian');
    }

    //根据设备号获取设备详情接口
	public function getSGoodsInfo()
    {
		global $_W, $_GPC;

        //验证令牌
        if(empty($_GPC['token']) || !$this->checkToken($_GPC['token']))
        {
            show_json(0, '参数有误');
        }

        $number = $_GPC['number'];
        if(empty($number))
        {
            show_json(0, '参数有误');
        }

        $sgoods = m('sgoods')->getDetail($number);
        if(empty($sgoods))
        {
            show_json(0, '设备不存在');
        }

        $type = m('sgoods')->getDetailToTableById($this->sGoodsTypeTable, $sgoods['type_id']);
        $brand = m('sgoods')->getDetailToTableById($this->sGoodsBrandsTable, $sgoods['brands_id']);
        $store = m('sgoods')->getDetailToTableById($this->mendianTable, $sgoods['mendian_id'], array('mendianname', 'description'));

        $res = array(
            'number' => $sgoods['docking_number'],
            'title' => $sgoods['name'],
            'cate' => $type['name'],
            'brand' => $brand['name'],
            'logo' => tomedia($sgoods['thumb']),
            'store' => $store['mendianname'],
            'desc' => $store['description'],
            'iswork' => $store['isnormal'],
        );

        show_json(1, array('data'=>$res));
	}

    //根据设备号获取价格套餐接口
    public function getSGoodsPrices()
    {
        global $_W, $_GPC;

        //验证令牌
        if(empty($_GPC['token']) || !$this->checkToken($_GPC['token']))
        {
            show_json(0, '参数有误');
        }

        $number = $_GPC['number'];
        if(empty($number))
        {
            show_json(0, '参数有误');
        }

        $data = m('sgoods')->getTypePriceListByGNumber($number);

        $res = array();
        if(!empty($data) && is_array($data))
        {
            foreach ($data as $index=>$item)
            {
                $res[$index]['priceid'] = $item['id'];
                $res[$index]['title'] = $item['name'];
                $res[$index]['thumb'] = tomedia($item['thumb']);
                $res[$index]['minutes'] = intval($item['usetime']);
                $res[$index]['price'] = number_format($item['price'], 2);
                $res[$index]['marketPrice'] = number_format($item['market_price'], 2);
                $res[$index]['score'] = intval($item['most_reduce_price'], 2);
                $res[$index]['desc'] = $item['description'];
            }
        }

        show_json(1, array('data'=>$res));
    }

    //根据设备号开启设备
    public function open()
    {
        global $_W, $_GPC;

        //验证令牌
        if(empty($_GPC['token']) || !$this->checkToken($_GPC['token']))
        {
            show_json(0, '参数有误');
        }

        $number = $_GPC['number'];
        if(empty($number))
        {
            show_json(0, '参数错误');
        }

        //开启设备
        $res = m('sgoods')->openSGoodsByNumber($number);
        if($res == -1)
        {
            show_json(0, '您购买该设备的使用权');
        }

        if($res)
        {
            show_json(1, '开启成功');
        }
        else
        {
            show_json(0, '设备开启失败');
        }
    }

    //根据设备号关闭设备
    public function close()
    {
        global $_W, $_GPC;

        //验证令牌
        if(empty($_GPC['token']) || !$this->checkToken($_GPC['token']))
        {
            show_json(0, '参数有误');
        }

        $number = $_GPC['number'];
        if(empty($number))
        {
            show_json(0, '参数错误');
        }

        //关闭设备
        $res = m('sgoods')->closeSGoodsByNumber($number);
        if($res == -1)
        {
            show_json('您购买该设备的使用权');
        }

        if($res == -2)
        {
            show_json('该设备还未开启');
        }

        if($res)
        {
            show_json(1, '关闭成功');
        }
        else
        {
            show_json(0, '关闭失败');
        }
    }

    //设备使用时间页面
    public function usetime()
    {
        global $_W, $_GPC;

        //获取令牌
        $token = $this->getToken();

        include $this->template();
    }

    //获取使用中设备
    public function onSGoods()
    {
        global $_W, $_GPC;

        //验证令牌
        if(empty($_GPC['token']) || !$this->checkToken($_GPC['token']))
        {
            show_json(0, '参数有误');
        }

        $sgoods = m('sgoods')->getInUseSgoodsByUid();
        if(empty($sgoods)){
            show_json(0, '没有开启中的设备');
        }

        show_json(1, array('data'=>$sgoods));
    }

    //获取设备种类详情
    public function getTypeDetail(){

        global $_W, $_GPC;

        //验证令牌
        if(empty($_GPC['token']) || !$this->checkToken($_GPC['token']))
        {
            show_json(0, '参数有误');
        }

        $number = $_GPC['number'];
        if(empty($number))
        {
            show_json(0, '参数错误');
        }

        $sgoods = m('sgoods')->getDetail($number, array('type_id'));
        if(empty($sgoods['type_id'])){
            show_json(0, '没有设备种类信息');
        }

        $type = m('sorder')->getDetailToTableById($this->sGoodsTypeTable, $sgoods['type_id']);
        if(empty($type)){
            show_json(0, '没有设备种类信息');
        }

        $res = array(
            'name' => $type['name'],
            'thumb' => tomedia($type['logo']),
            'content' => htmlspecialchars_decode($type['description'])
        );
        show_json(1, $res);
    }

}
